<?php

declare(strict_types=1);

namespace app\validate\product;

use think\Validate;

class ProdValidate extends Validate
{
    protected $rule = [
        'id' => 'integer|gt:0',
        'seller_org_id' => 'require|integer|gt:0',
        'seller_user_id' => 'integer|gt:0',
        'region_id' => 'integer|gt:0',
        'category_id' => 'require|integer|gt:0',
        'brand_id' => 'integer|gt:0',
        'title' => 'max:255',
        'subtitle' => 'max:255',
        'model' => 'require|max:120',
        'sku_code' => 'max:120',
        'cover_image' => 'max:255',
        'unit' => 'max:20',
        'min_order_qty' => 'integer|gt:0',
        'step_qty' => 'integer|gt:0',
        'default_price' => 'float',
        'default_stock' => 'integer|egt:0',
        'weight_value' => 'float',
        'weight_unit' => 'max:10',
        'size_length' => 'float',
        'size_width' => 'float',
        'size_height' => 'float',
        'origin_country' => 'max:80',
        'manufactured_at' => 'date',
        'summary' => 'max:512',
        'remark' => 'max:255',
        'status' => 'in:0,1,2,3',
        'audit_status' => 'in:0,1,2',
        'publish_time' => 'date',
    ];

    protected $message = [
        'seller_org_id.require' => '卖家组织不能为空',
        'seller_org_id.integer' => '卖家组织格式不正确',
        'seller_org_id.gt' => '卖家组织格式不正确',
        'category_id.require' => '分类不能为空',
        'category_id.integer' => '分类格式不正确',
        'category_id.gt' => '分类格式不正确',
        'model.require' => '商品型号不能为空',
        'title.max' => '商品标题不能超过255个字符',
        'sku_code.max' => 'SKU编码不能超过120个字符',
        'default_price.float' => '默认价格格式不正确',
        'default_stock.integer' => '库存必须为整数',
        'default_stock.egt' => '库存不能为负数',
        'status.in' => '商品状态不正确',
        'audit_status.in' => '审核状态不正确',
    ];

    protected $scene = [
        'create' => [
            'seller_org_id',
            'seller_user_id',
            'region_id',
            'category_id',
            'brand_id',
            'title',
            'subtitle',
            'model',
            'sku_code',
            'cover_image',
            'default_price',
            'default_stock',
            'unit',
            'min_order_qty',
            'step_qty',
            'weight_value',
            'weight_unit',
            'size_length',
            'size_width',
            'size_height',
            'origin_country',
            'manufactured_at',
            'summary',
            'description',
            'attachments',
            'attributes',
            'gallery',
            'videos',
            'remark',
            'status',
            'audit_status',
            'publish_time',
        ],
        'update' => [
            'id',
            'seller_org_id',
            'seller_user_id',
            'region_id',
            'category_id',
            'brand_id',
            'title',
            'subtitle',
            'model',
            'sku_code',
            'cover_image',
            'default_price',
            'default_stock',
            'unit',
            'min_order_qty',
            'step_qty',
            'weight_value',
            'weight_unit',
            'size_length',
            'size_width',
            'size_height',
            'origin_country',
            'manufactured_at',
            'summary',
            'description',
            'attachments',
            'attributes',
            'gallery',
            'videos',
            'remark',
            'status',
            'audit_status',
            'publish_time',
        ],
    ];

    protected function sceneCreate(): self
    {
        return $this->only($this->scene['create'])
            ->append('sku_code', 'max:120|unique:prod,sku_code');
    }

    protected function sceneUpdate(): self
    {
        return $this->only($this->scene['update'])
            ->append('id', 'require|integer|gt:0');
    }
}
